package com.yb.core.util;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;

import java.io.IOException;

public class YbSecureUtil {

    static String encre="VYI2k+XSpNpY+gGVSaEOvyfB4dYr2CbVXG9u61CJc5bBnA1YsK7vxFxVNvKO3DBeYt2S5rFsv9y3zzc3FV/L5JFoqdPefooUJpKuQcK4FSt8HE3g4Sze6Js3HBfU5ZKWNUzoJmXZ67vpbQ2QxsjcOYtckry9MgiIFDWbCPzOe4M=";
    public static void main(String[] args) throws IOException {
//        KeyPair pair = SecureUtil.generateKeyPair("RSA");
//        PrivateKey privateKey = pair.getPrivate();
//        PublicKey publicKey = pair.getPublic();
//        String path = System.getProperty("user.dir");
//
//        YbFileWriter.writeString(path,"login-private.txt", Base64.encode(privateKey.getEncoded()));
//        YbFileWriter.writeString(path,"login-public.txt", Base64.encode(publicKey.getEncoded()));
//        System.out.println(path);



        String privateKey = YbFileReader.readAsString(YbSecureUtil.class.getClassLoader().getResource("login-private.txt").getPath());
        String publicKey = YbFileReader.readAsString(YbSecureUtil.class.getClassLoader().getResource("login-public.txt").getPath());

        System.out.println(privateKey);
        System.out.println(publicKey);

        RSA rsa = new RSA(privateKey, publicKey);
//
//        System.out.println(rsa.getPrivateKey().getFormat());
//        System.out.println(rsa.getPublicKey().getFormat());
//        String content="hello world";
//        encPriDecPub(rsa,content);
//        encPubDecPri(rsa,content);
//
//
//        System.out.println(decryptRSAPrivate(rsa, encre));

//        System.out.println(DesensitizedUtil.mobilePhone("18049531999"));
        System.out.println(encryptRSAPublic(rsa, "18501349133"));
        System.out.println(encryptRSAPublic(rsa, "15502618017"));
    }

    public static void encPriDecPub(RSA rsa,String content){
        String encrstr = encryptRSAPrivate(rsa, content);
        System.out.println(encrstr);
        System.out.println(decryptRSAPublic(rsa,encrstr));
    }


    public static void encPubDecPri(RSA rsa,String content){
        String encrstr = encryptRSAPublic(rsa, content);
        System.out.println(encrstr);
        System.out.println(decryptRSAPrivate(rsa,encrstr));
    }
    public static String encryptRSAPrivate(RSA rsa,String content){
        byte[] encrypted = rsa.encrypt(StrUtil.bytes(content, CharsetUtil.CHARSET_UTF_8), KeyType.PrivateKey);
        return Base64.encode(encrypted);
    }

    public static String decryptRSAPublic(RSA rsa,String content){
        byte[] decrypt = rsa.decrypt(Base64.decode(content), KeyType.PublicKey);
        return new String(decrypt, CharsetUtil.CHARSET_UTF_8);
    }

    public static String encryptRSAPublic(RSA rsa,String content){
        byte[] encrypted = rsa.encrypt(StrUtil.bytes(content, CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
        return Base64.encode(encrypted);
    }

    public static String decryptRSAPrivate(RSA rsa,String content){
        byte[] decrypt = rsa.decrypt(Base64.decode(content), KeyType.PrivateKey);
        return new String(decrypt, CharsetUtil.CHARSET_UTF_8);
    }
}
